<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #box {
        width: 800px;
        height: 500px;
        border: 1px solid #000;
        margin: auto;
      }
    </style>
  </head>
  <body>
    <div id="box">
      <form action="">
        id <input type="text" name="id" id="id" />
        汽车名字
        <input type="text" name="username" id="username" value="111" />
        价格<input type="text" name="carPrice" id="carPrice" value="222" />
        颜色<input type="text" name="carColor" id="carColor" value="333" />
      </form>
      <button id="btn">点击发出请求</button>
      <button id="del">删除</button>
      <button id="xiu">修改</button>
      <button id="sea">搜索</button>
    </div>
    <script src="./node_modules/jquery/dist/jquery.js"></script>
    <script>
      // jQuery函数，确保DOM完全加载后执行
      $(function () {
        // 按钮点击事件
        $("#btn").click(function (index) {
          $.ajax({
            url: "http://localhost:3000/post/car", //服务器端点URL
            type: "post",
            // 汽车数据
            data: {
              username: $("#username").val(),
              pwe: $("#carPrice").val(),
              carColor: $("#carColor").val(),
            },
            dataType: "json",
            success: function (data) {
              console.log(data);
            },
            error: function (err) {
              console.log(err.responseJSON);
            },
          });
        });
        $("#del").click(function () {
          var id = $("#id").val();
          $.ajax({
            url: `http://localhost:3000/delete/car/${id}`,
            type: "delete",
            success: function (data) {
              console.log(data);
              // window.location.href = `http://localhost:3000/api/cars/${id}`;
            },
            error: function (err) {
              console.log(err.responseJSON);
            },
          });
        });
        $("#xiu").click(function () {
          var id = $("#id").val();

          $.ajax({
            url: `http://localhost:3000/put/car/${id}`,
            type: "put",
            data: {
              username: $("#username").val(),
              pwe: $("#carPrice").val(),
              carColor: $("#carColor").val(),
            },
            dataType: "json",
            success: function (res) {
              console.log(res);
            },
            error: function (err) {
              console.log(err.responseJSON);
            },
          });
        });
        $("#sea").click(function () {
          var id = $("#id").val();
          $.ajax({
            url: `http://localhost:3000/get/cars/${id}`,
            type: "get",
            success: function (data) {
              console.log(data);
            },
            error: function (err) {
              console.log(err.responseJSON);
            },
          });
          // window.location.href = `http://localhost:3000/api/carsss/${id}`;
        });
      });
    </script>
  </body>
</html>
